<template>
  <div class="permission-container">
    <div class="app-container">
      <el-card>
        <el-row type="flex" justify="end">
          <el-col :span="2"><el-button type="primary" @click="showAddDialog(1, '0')">添加权限</el-button></el-col>
        </el-row>
        <!-- 表格 -->
        <el-table border stripe :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button v-if="row.type === 1" size="mini" type="primary" @click="showAddDialog(2, row.id)">添加权限</el-button>
              <el-button size="mini" type="warning" @click="showEdit(row.id)">查看权限</el-button>
              <el-button size="mini" type="danger" @click="delPer(row.id)">删除权限</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 对话框 -->
        <el-dialog :visible="showDialog" :title="title" @close="showDialog = false">
          <!-- 表单内容 -->
          <el-form label-width="100px">
            <el-form-item label="权限名称">
              <el-input v-model="formData.name" />
            </el-form-item>
            <el-form-item label="权限标识">
              <el-input v-model="formData.code" />
            </el-form-item>
            <el-form-item label="权限描述">
              <el-input v-model="formData.description" />
            </el-form-item>
            <el-form-item label="权限启用">
              <el-switch
                v-model="formData.enVisible"
                active-text="启用"
                active-value="1"
                inactive-text="不启用"
                inactive-value="0"
              />
            </el-form-item>
          </el-form>
          <template #footer>
            <div style="text-align: right;">
              <el-button @click="showDialog = false">取消</el-button>
              <el-button type="primary" @click="handleSubmit">确定</el-button>
            </div>
          </template>
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import { reqGetPermissionList, reqAddPermission, reqDelPermission, reqGetPermissionDetail, reqUpdatePermission } from '@/api/permission'
import { tranListToTreeData } from '@/utils/index'
export default {
  name: 'Permission',
  data() {
    return {
      list: [],
      title: '新增权限',
      showDialog: false, // 控制对话框显示的布尔值
      formData: {
        enVisible: '0', // 不开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      }
    }
  },
  created() {
    this.getPermissions()
  },
  methods: {
    // 获取所有权限点
    async getPermissions() {
      const { data } = await reqGetPermissionList()
      this.list = tranListToTreeData(data, '0')
    },
    // 点击添加按钮展示对话框
    showAddDialog(type, pid) {
      this.showDialog = true
      // 将type和pid赋值
      this.formData.type = type
      this.formData.pid = pid
      this.title = '新增权限'
    },
    // 点击确定提交
    async handleSubmit() {
      if (this.formData.id) {
        await reqUpdatePermission(this.formData)
      } else {
        // 1. 预校验 省略
        // 2. 发送请求
        await reqAddPermission(this.formData)
      }
      // 3. 关闭对话框
      this.showDialog = false
      // 4. 重新渲染
      this.getPermissions()
      // 5. 重置表单
      this.formData = {
        enVisible: '0', // 不开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      }
    },
    // 删除权限
    delPer(id) {
      // 1. 确认提示
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        type: 'warning'
      }).then(async() => {
        // 2. 发送请求
        await reqDelPermission(id)
        // 3. 重新渲染
        this.getPermissions()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 点击编辑展示对话框并回显数据
    async showEdit(id) {
      // 1. 查询权限信息
      const { data } = await reqGetPermissionDetail(id)
      // 2. 将查询的信息赋值给formData
      this.formData = data
      // 3. 展示对话框
      this.showDialog = true
      this.title = '编辑权限'
    }
  }
}
</script>

<style>

</style>
